﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;
using System.Web.UI;

namespace XMK.Tadong.Web.Language
{
    public class LanguageDictionary
    {
        private Dictionary<string, string> langList;

        public LanguageDictionary(string code, HttpServerUtility server)
        {
            string fileExplorerPath = ConfigurationManager.AppSettings.Get("LanguagePath");
            string basePath = fileExplorerPath + "/Language";

            langList = new Dictionary<string, string>();
            string filePath = server.MapPath(Path.Combine(basePath, code + ".txt"));

            if (File.Exists(filePath))
            {
                try
                {
                    using (StreamReader sr = new StreamReader(filePath))
                    {
                        while (sr.Peek() >= 0)
                        {
                            string line = sr.ReadLine();
                            try
                            {
                                //Try to get the key value to the dictionary
                                string[] values = line.Split("=>".ToCharArray());
                                langList.Add(values[0], values[2]);
                            }
                            catch (Exception e)
                            {
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }

        /// <summary>
        /// Retrieve the matching value
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string Get(string name)
        {
            try
            {
                return langList[name];
            }
            catch (Exception ex)
            {
                return "";
            }
        }
    }
}